package com.abyss.transformation.datastream;


import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * reduce算子演示
 */
public class StreamReduceDemo {
    public static void main(String[] args) throws Exception {
        // 1. Env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 2. Socket source
        DataStreamSource<String> socketTextStream = env.socketTextStream("node1", 9999);

        // 3. 将单词数据转变为元组 <单词,1>
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordWithOne = socketTextStream.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] string = value.split(" ");
                for (String word : string) {
                    out.collect(Tuple2.of(word, 1));
                }
            }
        });

        // 4. keyby
        KeyedStream<Tuple2<String, Integer>, Tuple> keyedStream = wordWithOne.keyBy(0);

        // 5. 进行聚合reduce
        SingleOutputStreamOperator<Tuple2<String, Integer>> result = keyedStream.reduce(new ReduceFunction<Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> reduce(Tuple2<String, Integer> value1, Tuple2<String, Integer> value2) throws Exception {
                return Tuple2.of(value1.f0, value1.f1 + value2.f1);
            }
        });

        // 6. print and execute
        result.print();
        env.execute();
    }
}